package org.jradweb.core.utils;
/**
 * String Utils for Generator
 *
 * Created On 2007-12-02
 * @author <a href="mailto:wagzhi@gmail.com">Paul Wang</a>
 * @version $Id: StringUtils.java 165 2009-01-27 15:19:44Z wagzhi $
 */
public class StringUtils {
	/**
	 * convert 'xxx.xxxx' to 'xxx/xxxx'
	 * 
	 * @param packageName
	 * @return
	 */
	public static String packageToPath(String packageName){
		return packageName.replace('.', '/');
	}
	/**
	 * convert 'Xxxx' to 'xxxx'
	 * 
	 * @param name
	 * @return
	 */
	public static String lowerCaseFirst(String name){
		if(name==null||name.length()==0)
			return name;
		
		String first=name.substring(0,1);
		String lowFirst=first.toLowerCase();
		return lowFirst+name.substring(1,name.length());
	}
	
	/**
	 * convert 'xxxx' to 'Xxxx'
	 * 
	 * @param name
	 * @return
	 */
	public static String upperCaseFirst(String name){
		if(name==null||name.length()==0)
			return name;
		
		String first=name.substring(0,1);
		String upperFirst=first.toUpperCase();
		return upperFirst+name.substring(1,name.length());
	}
	/**
	 * convert 'AbcDef' to 'abc_def'
	 * @param name
	 * @return
	 */
	public static String lowerCaseName(String name){
		if(name==null||name.length()==0)
			return name;
		
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<name.length();i++){
			char c=name.charAt(i);
			String sc=""+c;
			
			if(sc.matches("[A-Z]{1}")){
				if(i==0)
					sc=sc.toLowerCase();
				else 
					sc="_"+sc.toLowerCase();
			}
			
			sb.append(sc);
		}
		
		return sb.toString();
	}
	
	public static String trimToNull(String str){
		String newStr=str==null?null:org.apache.commons.lang.StringUtils.trimToNull(str);
		newStr= replaceOtherChar(str);
		return newStr==null?null:org.apache.commons.lang.StringUtils.trimToNull(newStr);
	}
	public static String trimToEmpty(String str){
		String newStr=str==null?null:org.apache.commons.lang.StringUtils.trimToEmpty(str);
		newStr= replaceOtherChar(str);
		return newStr==null?null:org.apache.commons.lang.StringUtils.trimToEmpty(newStr);
	}
	/*
	 * 替换特殊字符
	 */
	private static String replaceOtherChar(String str){
		if(str==null){
			return null;
		}
		str=str.replaceAll("^([\\s　]*)|([\\s　]*)$", "");
		str=str.replace("^([　]*)|([　]*)$", "");
		return str;
	}
	

	/**
	 *  得到一个字符串显示的长度,一个汉字或日韩文长度为2,英文字符长度为1 
	 * */
	public static int getShowLenth(String s) {
		if (s == null) {
			return 0;
		}
		char[] c = s.toCharArray();
		int len = 0;
		for (int i = 0; i < c.length; i++) {
			len++;
			if (!isLetter(c[i])) {
				len++;
			}
		}
		return len;
	}
	/**
	 * 根据显示长度截取字符串
	 * @param origin
	 * @param showLength
	 * @return
	 */
	public static String getSubStringByShowLength(String origin, int showLength) {
		if (origin == null || origin.length() < 1 || showLength < 1) {
			return "";
		}
		if (showLength > getShowLenth(origin)) {
			return origin;
		}
		String result = "";
		int length = 0;
		for (char c : origin.toCharArray()) {
			length++;
			if (!isLetter(c)) {
				length++;
			}
			if (length <= showLength) {
				result = result + String.valueOf(c);
			} else {
				return result;
			}
		}
		return result;
	}
	
	/**
	 * 判断一个字符是Ascill字符还是其它字符（如汉，日，韩文字符） 
	 * */
	public static boolean isLetter(char c) {
		int k = 0x80;
		return c / k == 0 ? true : false;
	}
	
	/**
	 * get class name withot package,
	 * for example Class java.lang.String will return "String"
	 * @param clazz
	 * @return
	 */
	public static String getClassName(Class clazz){
		String fullName=clazz.getName();
		int index=fullName.lastIndexOf('.');
		if(index<0)
			return fullName;
		
		return fullName.substring(index+1);
	}
	
	/**
	 * get class name withot package,
	 * for example Class java.lang.String will return "String"
	 * @param clazz
	 * @return
	 */
	public static String getLowerCaseFirstClassName(Class clazz){
		String fullName=clazz.getName();
		int index=fullName.lastIndexOf('.');
		if(index<0)
			return fullName;
		
		String className= fullName.substring(index+1);
		
		return lowerCaseFirst(className);
	}
	
//	public static void main(String[] args){
//		String str="中 　";
//		String str1=StringUtils.trimToNull(str);
//		System.out.println("("+str1+")");
//	}

}
